package com.example.prom.qianduoduo.y2025.controller;

import com.alibaba.excel.EasyExcel;
import com.example.prom.qianduoduo.y2025.model.SacinfoModel;
import com.example.prom.strategy.excel.CustomCellStyleStrategy;
import com.example.prom.strategy.excel.CustomColumnWidthStrategy;

import java.util.*;
import java.util.stream.Collectors;

/**
 * Description: 对比两个 Excel 文件的数据差异（使用 recordNo 备案号进行比对）
 *
 * @author DangWen
 */
public class SacinfoCompareData {

//    private static final String FILE_1 = "D:\\workspace-pycharm\\spider-out\\123.xlsx";
//    private static final String FILE_2 = "D:\\workspace-pycharm\\spider-out\\爬取数据78300.xlsx";
    private static final String FILE_1 = "D:\\workspace-pycharm\\spider-out\\456.xlsx";
    private static final String FILE_2 = "D:\\workspace-pycharm\\spider-out\\123文件中含有的_爬取数据78300不包含的.xlsx";

    public static void main(String[] args) {
        // 读取两个文件的数据
        List<SacinfoModel> list1 = readExcel(FILE_1);
        List<SacinfoModel> list2 = readExcel(FILE_2);

        // 标准化 recordNo：去除首尾空白
        list1.forEach(model -> model.setRecordNo(normalizeRecordNo(model.getRecordNo())));
        list2.forEach(model -> model.setRecordNo(normalizeRecordNo(model.getRecordNo())));

        // 使用 recordNo 作为唯一标识构建 Map
        Map<String, SacinfoModel> map1 = list1.stream()
                .collect(Collectors.toMap(SacinfoModel::getRecordNo, m -> m, (existing, replacement) -> existing));
        Map<String, SacinfoModel> map2 = list2.stream()
                .collect(Collectors.toMap(SacinfoModel::getRecordNo, m -> m, (existing, replacement) -> existing));

        // 找出差异
        List<SacinfoModel> onlyInFile1 = list1.stream()
                .filter(m -> !map2.containsKey(m.getRecordNo()))
                .collect(Collectors.toList());

        List<SacinfoModel> onlyInFile2 = list2.stream()
                .filter(m -> !map1.containsKey(m.getRecordNo()))
                .collect(Collectors.toList());

        // 输出结果并写入 Excel（如果存在）
        if (!onlyInFile1.isEmpty()) {
            writeExcel(onlyInFile1, "456中额外包含的.xlsx");
        }

        if (!onlyInFile2.isEmpty()) {
            writeExcel(onlyInFile2, "比对数据额外包含的.xlsx");
        }

        System.out.println("对比完成！");
    }

    /**
     * 读取 Excel 文件内容
     */
    private static List<SacinfoModel> readExcel(String filePath) {
        return EasyExcel.read(filePath)
                .head(SacinfoModel.class)
                .sheet()
                .doReadSync();
    }

    /**
     * 标准化 recordNo：移除所有前后空格
     */
    private static String normalizeRecordNo(String recordNo) {
        if (recordNo == null) return "";
        return recordNo.trim();
    }

    /**
     * 写入差异数据到新的 Excel 文件
     */
    private static void writeExcel(List<SacinfoModel> data, String outputFileName) {
        String outputPath = "D:\\workspace-pycharm\\spider-out\\" + outputFileName;
        EasyExcel.write(outputPath, SacinfoModel.class)
                .sheet()
                .registerWriteHandler(new CustomColumnWidthStrategy())
                .registerWriteHandler(new CustomCellStyleStrategy())
                .doWrite(data);
        System.out.println("已生成差异文件：" + outputPath);
    }
}
